<?php

namespace App\Http\Controllers\ApiHospital;

use App\Http\Controllers\Controller;
use App\Models\Hospital;
use App\Models\HospitalAdminUsers;
use App\Models\old\OldDoctor;
use App\Models\old\UserLogin;
use App\Providers\SmsSend;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;

class LoginController extends Controller
{
    public function pushCode(Request $request){
        $phone = $request->input('phone');
        SmsSend::sendSms($phone);

        return response()->json(['code'=>200,'msg'=>'成功']);
    }
    public function login(Request $request)
    {
        $phone = $request->input('phone');
        $code = $request->input('code');
//        $vCode = Cache::get('vcode'.$phone);
//        if($code != $vCode){
//            return response()->json(['code'=>500,'msg'=>'验证码错误']);
//        }

        try {


            $userLogin = HospitalAdminUsers::query()
                ->where('username', $phone)
                //->where('type', 1)
                ->first();
            if (!$userLogin) {
                return response()->json(['code' => 500, 'msg' => '手机号码未注册']);
            }
            $doctors = Hospital::query()->where('id', $userLogin->hospital_id)->first();
            if (!$doctors) {
                return response()->json(['code' => 500, 'msg' => '账号不存在']);
            }
            $guard = Auth::guard('api-hospital');
            $token = $guard->login($userLogin);
        }catch (\Exception $e){
            var_dump($e->getMessage());exit;
        }

        return response()->json(['code'=>200,'msg'=>'成功','token'=>$token]);
    }
}
